﻿using System.Configuration;
using System.Xml.Serialization;
using System.IO;
using System.Web;
using Sissonix.ExtRest.WADL;
using Sissonix.ExtRest;

/*Notes: http://www.4guysfromrolla.com/articles/032807-1.aspx

 * On the context.Request object, here's an example of what you'll find:
    Path=/user/rest/
    QueryString=_dc=12312351535&start=0&limit=10
    RawUrl=/user/rest/?_dc=12312351535&start=0&limit=10
    RequestType="GET"
    URL=http://localhost:28624/user/rest/?_dc=12312351535&start=0&limit=10
    Headers["Host"]=http://localhost:28624
 */
namespace Sissonix.ExtRest.Configuration
{
   /// <summary>
    /// Configuration section for the ExtRest engine.
    /// </summary>
    public class ExtRestConfig : ConfigurationSection
    {
        private application _wadlApp;

        public application WADLApp
        {
            get {
                if (_wadlApp == null)
                    ReloadWADL();

                return _wadlApp; 
            }
        }
        
        public static ExtRestConfig config { get; set; }

        /// <summary>
        /// Creates the singleton.
        /// </summary>
        static ExtRestConfig()
        {
            config = (ExtRestConfig)(ConfigurationManager.GetSection("ExtRest"));
        }
       
        public ExtRestConfig() : base() {
        }

        public void ReloadWADL() {
            try
            {
                //string wadlPath = HttpContext.Current.Server.MapPath(config.WADLFilePath);
                string wadlPath = @"C:\MarkSisson\Clients\Sissonix\WADLTester\Sample.xml";

                XmlSerializer serializer = new XmlSerializer(typeof(application));
                TextReader reader = new StreamReader(wadlPath);
                _wadlApp = (application)serializer.Deserialize(reader);
                reader.Close();
            }
            catch (System.Exception e)
            {
                string m = e.Message;
            }
        }

        [ConfigurationProperty("WADLFilePath")]
        public string WADLFilePath
        {
            get { return (string)this["WADLFilePath"]; }
        }

        /// <summary>
        /// This static method should be added to the Global.asax
        /// Application_Start() method to make sure that the proper
        /// listeners are added to the route table.
        /// It is add like this:
        ///   ExtRestConfig.AppStart();
        /// </summary>
        public static void AppStart()
        {
            Class1 c = new Class1();
            string zzz = c.Boo2;

            resource rr = new resource();
            string asdf = rr.ProcessingClass;

            application a = ExtRestConfig.config.WADLApp;

            resources rrrrr = a.CurrentResources;
            
            string b = a.resources[0].@base.StripFirstLastDelims();

            var route = new System.Web.Routing.Route(b + "/{*path}",
                new ExtRest.RestRouteHandler(b));
            System.Web.Routing.RouteTable.Routes.Add(route);
        }

        [ConfigurationProperty("serverName")]
        public string ServerName
        {
            get { return (string)this["serverName"]; }
        }

        [ConfigurationProperty("databaseName")]
        public string DatabaseName
        {
            get { return (string)this["databaseName"]; }
        }

        [ConfigurationProperty("userId")]
        public string UserId
        {
            get { return (string)this["userId"]; }
        }

        [ConfigurationProperty("password")]
        public string Password
        {
            get { return (string)this["password"]; }
        }

        [ConfigurationProperty("appName")]
        public string AppName
        {
            get { return (string)this["appName"]; }
        }

     }
}